﻿using Furion.DistributedIDGenerator;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using net8.Models;
using net8.Models.Wangzhan;
using net8.Views;

namespace net8.Controllers.Wangzhan
{
    /// <summary>
    /// 网站类型管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WzTypeController : ControllerBase
    {
        [HttpPost]
        public void Save([FromBody] WzType model)
        {
            model.Id = model.Id == Guid.Empty ? IDGen.NextID() : model.Id;
            model.AddTime = DateTime.Now;
            model.UpdateTime = DateTime.Now;
            model.IsDel = false;
            DbHelper.Db.Storageable(model).ExecuteCommand();
        }

        [HttpDelete]
        public void Delete(Guid id)
        {
            int a = DbHelper.Db.Deleteable<WzType>().Where(t => t.Id == id).ExecuteCommand();
        }

        [HttpGet]
        public List<WzType> GetList()
        {
            return DbHelper.Db.Queryable<WzType>().ToTree(t => t.Children, t => t.ParentId, Guid.Empty);
        }

        [HttpGet]
        [AllowAnonymous]
        public List<TreeSelect> GetTreeList()
        {
            return DbHelper.Db.Queryable<WzType>()
                .Select(t => new TreeSelect() { Value = t.Id, Label = t.Name, ParentId = t.ParentId })
                .ToTree(t => t.Children, t => t.ParentId, Guid.Empty);
        }
    }
}
